package com.feng.web;

import imageUtil.Image;
import imageUtil.ImageLoader;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.feng.util.web.ServletUtil;

public class userpic extends HttpServlet {

	private static final long serialVersionUID = 1L;


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		ServletUtil.forword(request, response, "user-pic");
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Map<String,String> map = ServletUtil.uploadFile(request);
		
		String fileName = map.get("fileName");
		
		File file = new File("C:/upload",fileName);
		
		Image img = ImageLoader.fromFile(file);
		if(img.getWidth() > 500) {
			img.getResizedToWidth(500).writeToFile(file);
		}
		
		ServletUtil.setSessionAttirbute(request, "userpicpath", fileName);
		
		ServletUtil.redirect(response, "userpic.jspx");
		
	}

}
